import { getAllNews,searchNews} from "../../api/news";

Page({
  data: {
    articles: [] ,// 存储文章数据
    mockData: [
      
      
    ]
  },

  onLoad() {
    // 模拟从后台获取文章数据
    this.getArticles();
  },
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      });
    }
  },
  async getArticles() {
    const res = await getAllNews()
    console.log('----ss',res);


    // 遍历数据，处理标题
    const processedArticles = res.data.map(item => {
      return {
        ...item,
        shortTitle: item.title.length > 20 ? item.title.slice(0, 20) + '...' : item.title
      };
    });
    const lastThreeArticles = processedArticles.slice(-3).map((article, index) => {
      return {
        ...article,
        index: processedArticles.length < 3 ? index + 1 : index + 1
      };
    });
    // 反转数组
  const reversedArticles = lastThreeArticles.reverse().map((item, index) => {
    return {
      ...item,
      index: index + 1
    };
  });
    // 设置数据
    this.setData({
      articles: reversedArticles
    });  
    
 
  },
 
    onMessageClick(){   
      wx.navigateTo({
        url: '/pages/manage_message/index' 
      })
    },

  

  
  onNotificeClick(event){
   
    wx.navigateTo({
      url: '/pages/manage_news/index' 
    })
  }
});
